home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / binaries / Windows / jsdk / bin / srun.c next >
Encoding:
C/C++ Source or Header  |  1997-07-18  |  3.1 KB  |  125 lines

  1. /*
  2.  * @(#)srun.c    1.4 97/03/03
  3.  * 
  4.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.0
  20.  */
  21.  
  22. /*
  23.  * SRUN - ServletRunner for testing servlets on Win32 systems. In order
  24.  * to run the server, a 1.0.2 compatible runtime must be found in the
  25.  * executable search path. The default name of the runtime executable
  26.  * to use is "java.exe" but can be overriden by setting JAVA_EXE.
  27.  */
  28.  
  29. #include <windows.h>
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <process.h>
  33. #include <direct.h>
  34.  
  35. #define JAVA_EXE "java.exe"
  36.  
  37. /*
  38.  * Return true if specified path refers to a directory.
  39.  */
  40. int isDirectory(char *path)
  41. {
  42.     int attr = GetFileAttributes(path);
  43.     return (attr != -1) && ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0);
  44. }
  45.  
  46. /*
  47.  * Returns true if specified path refers to a file.
  48.  */
  49. int isFile(char *path)
  50. {
  51.     int attr = GetFileAttributes(path);
  52.     return (attr != -1) && ((attr & FILE_ATTRIBUTE_DIRECTORY) == 0);
  53. }
  54.  
  55. /*
  56.  * Print error message and die.
  57.  */
  58. void error(char *msg)
  59. {
  60.     fprintf(stderr, "Fatal error: %s\n", msg);
  61.     exit(1);
  62. }
  63.  
  64. void
  65. main(int argc, char *argv[])
  66. {
  67.     char *java_exe, *jsdk_home, *cp;
  68.     char **args, **ap;
  69.     char buf[1024];
  70.     int r;
  71.  
  72.     /*
  73.      * JAVA_EXE if set is the file name of the 1.0.2 compatible Java runtime
  74.      * to use when running the server.
  75.      */
  76.     java_exe = getenv("JAVA_EXE");
  77.     if (java_exe == 0) {
  78.     java_exe = JAVA_EXE;
  79.     }
  80.  
  81.     /*
  82.      * JSDK_HOME if set is the directory where the Java Servlet Development
  83.      * Kit (JSDK) was installed. Otherwise, it defaults to the parent of the
  84.      * directory containing this executable.
  85.      */
  86.     jsdk_home = getenv("JSDK_HOME");
  87.     if (jsdk_home == 0) {
  88.     GetModuleFileName(0, buf, sizeof(buf));
  89.     cp = strrchr(buf, '\\') + 1;
  90.     strcpy(cp, "..");
  91.     jsdk_home = strdup(buf);
  92.     }
  93.     if (!isDirectory(jsdk_home)) {
  94.     error("Invalid setting for JSDK_HOME (not a directory).");
  95.     }
  96.  
  97.     /*
  98.      * Add JSDK classes to CLASSPATH.
  99.      */
  100.     cp = getenv("CLASSPATH");
  101.     sprintf(buf, "CLASSPATH=%s\\classes;%s\\lib\\classes.zip;%s",
  102.          jsdk_home, jsdk_home, cp != 0 ? cp : "");
  103.     putenv(strdup(buf));
  104.  
  105.     /*
  106.      * Gather arguments to pass to java.exe
  107.      */
  108.     ap = args = malloc((argc + 2) * sizeof(char *));
  109.     *ap++ = java_exe;
  110.     *ap++ = "sun.servlet.http.HttpServer";
  111.     while (--argc > 0) {
  112.     *ap++ = *++argv;
  113.     }
  114.     *ap = 0;
  115.  
  116.     /*
  117.      * Invoke Java runtime.
  118.      */
  119.     r = spawnvp(P_WAIT, args[0], args);
  120.     if (r != 0) {
  121.     perror(args[0]);
  122.     exit(1);
  123.     }
  124. }
  125.